package mbg.comment;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Set;

public class MyCommentGenerator implements CommentGenerator {

    /**
     * The properties.
     */
    private Properties properties;

    private SimpleDateFormat dateFormat;

    public MyCommentGenerator() {
        super();
        properties = new Properties();
        dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    public void addJavaFileComment(CompilationUnit compilationUnit) {
    }

    /**
     * 实体类对应的mapper.xml注释，mapper类不加注释，如有需要参考 DefaultCommentGenerator
     */
    public void addComment(XmlElement xmlElement) {

    }

    public void addRootComment(XmlElement rootElement) {

    }

    public void addConfigurationProperties(Properties properties) {
        this.properties.putAll(properties);

    }

    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {

    }

    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        String remarks = introspectedTable.getRemarks();
        String tablename = introspectedTable.getFullyQualifiedTableNameAtRuntime();
        addJavadocTag(topLevelClass, true, remarks, tablename);
    }

    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {

    }

    /**
     * 实体类字段注释
     */
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        String remarks = introspectedColumn.getRemarks();
        addJavadocTag(field, false, remarks);
    }

    /**
     * 实体类的静态字段
     */
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
        String remarks = introspectedTable.getRemarks();
        addJavadocTag(field, false, remarks);
    }

    /**
     * 实体类toString方法
     */
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {

    }

    /**
     * 实体类getter方法注释
     */
    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {

    }

    /**
     * 实体类setter注释
     */
    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {

    }

    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {

    }

    @Override
    public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {

    }

    @Override
    public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set) {

    }

    @Override
    public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {

    }

    @Override
    public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set) {

    }

    @Override
    public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {

    }

    /**
     * 所有的注释处理
     */
    protected void addJavadocTag(JavaElement javaElement, boolean dateFlag, String... remarks) {
        javaElement.addJavaDocLine("/**");
        // 循环遍历需要添加的注释
        if (remarks != null) {
            for (String remark : remarks) {
                javaElement.addJavaDocLine(" * " + remark);
            }
        }
        // 判断是否增加日期
        if (dateFlag) {
            javaElement.addJavaDocLine(" * " + getDateString());
        }
        javaElement.addJavaDocLine(" */");
    }

    protected String getDateString() {
        return dateFormat.format(new Date());
    }
}